package backend;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class BookInformation implements ProductInformation {
	private int product_id;
	private String authors;
	private String publisher;
	private Date published_date;
	private String ISBN_10;
	private String ISBN_13;
	private String language;
	
	public BookInformation(int product_id, String authors, String publisher,
			Date published_date, String isbn_10, String isbn_13, String language) {
		super();
		this.product_id = product_id;
		this.authors = authors;
		this.publisher = publisher;
		this.published_date = published_date;
		ISBN_10 = isbn_10;
		ISBN_13 = isbn_13;
		this.language = language;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + product_id;
		return result;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof BookInformation))
			return false;
		BookInformation other = (BookInformation) obj;
		if (product_id != other.product_id)
			return false;
		return true;
	}

	@Override
	public List<String> getValues() {
		List<String> resp = new ArrayList<String>();
		resp.add(new Integer(product_id).toString());
		resp.add(authors);
		resp.add(publisher);
		resp.add(published_date.toString());
		resp.add(ISBN_10);
		resp.add(ISBN_13);
		resp.add(language);
		return resp;
		
	}

	public BookInformation(int id) throws Exception {
		super();
		this.product_id=id;
		Connection.setServiceUrl("http://eiffel.itba.edu.ar/hci/service/Catalog.groovy");
		String[] arg = { "product_id=" +id };
		
		Document dom = Connection.connect("GetProduct", "GET", arg);
		NodeList nodes = dom.getElementsByTagName("product");
		Element e = (Element) nodes.item(0);
		this.authors = ((e.getElementsByTagName("authors")).item(0))
				.getTextContent();
		this.publisher = ((e.getElementsByTagName("publisher")).item(0))
				.getTextContent();
		this.language = ((e.getElementsByTagName("language")).item(0))
				.getTextContent();
		this.ISBN_10 = ((e.getElementsByTagName("ISBN_10")).item(0))
				.getTextContent();
		this.ISBN_13 = ((e.getElementsByTagName("ISBN_13")).item(0))
				.getTextContent();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String strFecha = ((e.getElementsByTagName("published_date")).item(0))
				.getTextContent();
		this.published_date= null;

		/*Esto puede tirar una ParseException*/
		this.published_date = dateFormat.parse(strFecha);


			
	}
	
}
